package cn.com.dpc.util;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

public class HttpGet {
	public static String getResult(String path, Context activity, String code)
			throws Exception {
		int id = path.indexOf("/", 8);
		String start = path.substring(7, id);
		String end = path.substring(id, path.length());

		ConnectivityManager conManager = (ConnectivityManager) activity
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		State mobile = conManager.getNetworkInfo(
				ConnectivityManager.TYPE_MOBILE).getState();
		State wifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		NetworkInfo mobileInfo = conManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		String type = mobileInfo.getExtraInfo();

		String sb = "";
		// 实例化一个HttpClient
		HttpClient httpClient = new DefaultHttpClient();
		// 设定目标站点 web的默认端口80可以不写的 当然如果是其它端口就要标明
		HttpHost httpHost = new HttpHost(start);

		org.apache.http.client.methods.HttpGet httpGet = new org.apache.http.client.methods.HttpGet(
				end);
		if (type != null && type.equalsIgnoreCase("cmwap")) {
			// 代理的设置
			HttpHost proxy = new HttpHost("10.0.0.172", 80);
			httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
					proxy);

		}
		// 执行
		HttpResponse response = httpClient.execute(httpHost, httpGet);

		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			sb = EntityUtils.toString(response.getEntity(), code);
		}

		return sb;
	}
}
